from message.api import MessageService
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer

import smtplib
from email.mime.text import MIMEText
from email.header import Header

serverPort = "7002"
sender = 'test3@dayscraft.cn'
authCode = '900900.com'

class MessageServiceHandler:
    def sendMobileMessage(self, mobile, message):
        print("send mobile："+mobile)
        print("send mobile message："+message)
        return True

    def sendEmailMessage(self, email, message):
        messageObj = MIMEText(message,"plain","utf-8")
        messageObj['From'] = sender
        messageObj['To'] = email
        messageObj['Subject'] = Header('yc开发环境','utf-8')
        try:
            smtpObj = smtplib.SMTP('smtp.dayscraft.cn')
            smtpObj.login(sender,authCode)
            smtpObj.sendmail(sender,[email],messageObj.as_string())
            smtpObj.quit()
            print("send mail success ："+email)
            return True
        except smtplib.SMTPException as ex:
            print("send mail failed ："+email)
            print(ex)
            return False

if __name__ == '__main__':
    handler = MessageServiceHandler()
    processor = MessageService.Processor(handler)
    transport = TSocket.TServerSocket("127.0.0.1",serverPort)
    tfactory = TTransport.TFramedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    print("message server start..........")
    server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
    server.serve()
    print("message server quit..........")


























